package com.uduemc.biso.master.backend.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.uduemc.biso.core.extities.center.SysServer;
import com.uduemc.biso.core.utils.JsonResult;
import com.uduemc.biso.master.backend.service.ServermanageService;

@Controller
@RequestMapping("/backend/servermanage")
public class BServermanageController {

	@Autowired
	private ServermanageService servermanageServiceImpl;

	@GetMapping("/addnode")
	public ModelAndView addnode(ModelAndView model) {
		model.setViewName("backend/servermanage/addnode");
		return model;
	}

	@GetMapping("/nodelist")
	public ModelAndView nodelist(ModelAndView model) {
		model.setViewName("backend/servermanage/nodelist");
		return model;
	}

	@GetMapping("/nodelist/infos")
	@ResponseBody
	public List<SysServer> nodelistInfos()
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		List<SysServer> serverByTypeStatus = servermanageServiceImpl.getServerByTypeStatus(0, (short) -1);
		return CollectionUtils.isEmpty(serverByTypeStatus) ? new ArrayList<>() : serverByTypeStatus;
	}

	@GetMapping("/nodelist/infosstatus/{status}")
	@ResponseBody
	public List<SysServer> nodelistInfos(@PathVariable("status") short status)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		List<SysServer> serverByTypeStatus = servermanageServiceImpl.getServerByTypeStatus(0, status);
		return CollectionUtils.isEmpty(serverByTypeStatus) ? new ArrayList<>() : serverByTypeStatus;
	}

	/**
	 * 新增分控服务器节点 ajax 动作
	 * 
	 * @param sysServer
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws JsonProcessingException
	 * @throws IOException
	 */
	@PostMapping("/nodelist/append")
	@ResponseBody
	public JsonResult nodelistAppend(@RequestBody SysServer sysServer)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		sysServer.setType(0);
		SysServer insert = servermanageServiceImpl.insert(sysServer);
		if (insert == null) {
			return JsonResult.illegal();
		}
		return JsonResult.messageSuccess("保存成功！", insert);
	}

	/**
	 * 更新分控服务器节点信息 ajax 动作
	 * 
	 * @param sysServer
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws JsonProcessingException
	 * @throws IOException
	 */
	@PostMapping("/nodelist/update")
	@ResponseBody
	public JsonResult nodelistUpdate(@RequestBody SysServer sysServer)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		sysServer.setType(0).setCreateAt(null);
		SysServer data = servermanageServiceImpl.getById(sysServer.getId());
		if (data == null) {
			return JsonResult.illegal();
		}
		SysServer update = servermanageServiceImpl.update(sysServer);
		if (update == null) {
			return JsonResult.illegal();
		}
		return JsonResult.messageSuccess("更新成功！", update);
	}

	/**
	 * 删除分控服务器节点信息，并且只有被关闭的数据才能删除
	 * 
	 * @param id
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws JsonProcessingException
	 * @throws IOException
	 */
	@PostMapping("/nodelist/delete")
	@ResponseBody
	public JsonResult nodelistDelete(@RequestParam("id") long id)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		SysServer sysServer = servermanageServiceImpl.getById(id);
		if (sysServer == null || sysServer.getStatus().shortValue() != (short) 2) {
			return JsonResult.illegal();
		}
		sysServer.setStatus((short) 4);
		SysServer update = servermanageServiceImpl.update(sysServer);
		if (update == null) {
			return JsonResult.illegal();
		}
		return JsonResult.messageSuccess("删除成功！", update);
	}

	@GetMapping("/accessip")
	public ModelAndView accessip(ModelAndView model) {
		model.setViewName("backend/servermanage/accessip");
		return model;
	}

	@GetMapping("/accessip/infos")
	@ResponseBody
	public List<SysServer> accessipInfos(ModelAndView model)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		model.setViewName("backend/servermanage/accessip");
		List<SysServer> serverByTypeStatus = servermanageServiceImpl.getServerByTypeStatus(1, (short) -1);
		return CollectionUtils.isEmpty(serverByTypeStatus) ? new ArrayList<SysServer>() : serverByTypeStatus;
	}

	@PostMapping("/accessip/append")
	@ResponseBody
	public JsonResult accessipAppend(@RequestParam("name") String name, @RequestParam("ipaddress") String ipaddress)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		SysServer sysServer = new SysServer();
		sysServer.setType(1).setName(name).setServerName("").setIpaddress(ipaddress).setDomain("").setLoginPort("80")
				.setStatus((short) 1);
		SysServer insert = servermanageServiceImpl.insert(sysServer);
		if (insert == null) {
			return JsonResult.illegal();
		}
		return JsonResult.messageSuccess("保存成功！", insert);
	}

	@PostMapping("/accessip/update")
	@ResponseBody
	public JsonResult accessipUpdate(@RequestParam("id") long id, @RequestParam("name") String name,
			@RequestParam("ipaddress") String ipaddress)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		SysServer sysServer = servermanageServiceImpl.getById(id);
		if (sysServer == null) {
			return JsonResult.illegal();
		}
		sysServer.setName(name).setIpaddress(ipaddress);
		SysServer update = servermanageServiceImpl.update(sysServer);
		if (update == null) {
			return JsonResult.illegal();
		}
		return JsonResult.messageSuccess("更新成功！", update);
	}

	@PostMapping("/accessip/delete")
	@ResponseBody
	public JsonResult accessipDelete(@RequestParam("id") long id)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		SysServer sysServer = servermanageServiceImpl.getById(id);
		if (sysServer == null) {
			return JsonResult.illegal();
		}
		SysServer delete = servermanageServiceImpl.deleteById(sysServer.getId());
		if (delete == null) {
			return JsonResult.illegal();
		}
		return JsonResult.messageSuccess("删除成功！", delete);
	}
}
